﻿using System;

using System.Collections.Generic;
using System.Text;
using Silvermoon.Controls.Base;
using System.Drawing;

namespace Silvermoon.Controls
{

    public struct GridOffsets
    {
        public int[] RowOffsets;
        public int[] ColOffsets;

        public GridOffsets(GridDefinition def, Size size)
        {
            int rows = def.Rows.Count;
            int cols = def.Columns.Count;
            RowOffsets = new int[rows];
            ColOffsets = new int[cols];

            int w = size.Width;
            int h = size.Height;

            ComputeColumns(def.Columns, w);
            ComputeRows(def.Rows, h);
        }

        private void ComputeColumns(AnimationGridUnitCollection cols, int w)
        {
            int fixedWidth = 0;
            float dynamicScale = 0f;
            foreach (GridUnit unit in cols)
            {
                if (unit.Type == GridUnitType.Fixed)
                {
                    int v = unit.fixedValue;
                    fixedWidth += v;
                }
                else
                {
                    float f = unit.dynamicValue;
                    dynamicScale += f;
                }
            }

            w -= fixedWidth;
            float fw = (float)w;
            int x = 0;
            int index = 0;
            foreach (GridUnit unit in cols)
            {
                if (unit.Type == GridUnitType.Fixed)
                {
                    int v = unit.fixedValue;
                    x += v;
                }
                else
                {
                    float f = unit.dynamicValue;
                    x += (int)(fw * f / dynamicScale + 0.5f);
                }
                ColOffsets[index++] = x;
            }
        }

        private void ComputeRows(AnimationGridUnitCollection rows, int h)
        {
            int fixedHeight = 0;
            float dynamicScale = 0f;
            foreach (GridUnit unit in rows)
            {
                if (unit.Type == GridUnitType.Fixed)
                {
                    int v = unit.fixedValue;
                    fixedHeight += v;
                }
                else
                {
                    float f = unit.dynamicValue;
                    dynamicScale += f;
                }
            }

            h -= fixedHeight;
            float fw = (float)h;
            int y = 0;
            int index = 0;
            foreach (GridUnit unit in rows)
            {
                if (unit.Type == GridUnitType.Fixed)
                {
                    int v = unit.fixedValue;
                    y += v;
                }
                else
                {
                    float f = unit.dynamicValue;
                    y += (int)(fw * f / dynamicScale + 0.5f);
                }
                RowOffsets[index++] = y;
            }
        }

    }

    internal class GridContainer : Decorator
    {
        public GridContainer()
            : base()
        {
        }
    }

}
